package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.IndexImg;
import com.powernode.model.Result;
import com.powernode.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RenBoQing
 * @date 2024年08月25日 13:56
 * @Description 轮播图控制器
 */

@Api(tags = "轮播图业务管理接口")
@RequestMapping("/admin/indexImg/")
@RestController
public class IndexImageController {
    @Autowired
    private IndexImgService indexImageService;

    /*
     * 分页条件获取所有轮播图
     * @author RenBoQing
     * @date 2024/8/25 14:05
     * @param current
     * @param size
     * @param status
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.IndexImg>>
     */
    @ApiOperation("分页条件获取所有轮播图")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> getAllIndexImageList(@RequestParam(defaultValue = "1", required = false) Long current, @RequestParam(defaultValue = "10", required = false) Long size, @RequestParam(required = false) Integer status) {
        // 构建分页对象
        Page<IndexImg> page = new Page<>(current, size);
        // 条件查询轮播图
        page = indexImageService.page(page, new LambdaQueryWrapper<IndexImg>().eq(ObjectUtil.isNotNull(status), IndexImg::getStatus, status).orderByDesc(IndexImg::getSeq));
        // 返回分页对象
        return Result.success(page);
    }

    /*
     * 新增轮播图
     * @author RenBoQing
     * @date 2024/8/25 14:40
     * @param indexImage
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("新增轮播图")
    @PostMapping("")
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public Result<String> saveIndexImage(@RequestBody IndexImg indexImage) {
        // 新增轮播图
        boolean save = indexImageService.saveIndexImg(indexImage);
        return Result.handle(save);
    }

    /*
     * 修改轮播图
     * @author RenBoQing
     * @date 2024/8/25 14:40
     * @param indexImage
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("修改轮播图")
    @PutMapping("")
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<String> updateIndexImage(@RequestBody IndexImg indexImage) {
        // 修改轮播图
        boolean update = indexImageService.modifyIndexImg(indexImage);
        return Result.handle(update);
    }
    /*
     * 根据id查询轮播图信息
     * @author RenBoQing
     * @date 2024/8/25 14:56
     * @param imgId
     * @return com.powernode.model.Result<com.powernode.domain.IndexImg>
     */
    @ApiOperation("根据id查询轮播图信息")
    @GetMapping("info/{imgId}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public Result<IndexImg> getIndexImageById(@PathVariable Long imgId) {
        return Result.success((IndexImg)indexImageService.getInfoById(imgId));
    }
    /*
     * 批量删除轮播图
     * @author RenBoQing
     * @date 2024/8/25 15:01
     * @param imgId
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("批量删除轮播图")
    @DeleteMapping("")
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<String> deleteIndexImageByIds(@RequestBody List<Long> imgIds) {
        // 批量删除轮播图
        boolean delete = indexImageService.deleteIndexImageByIds(imgIds);
        return Result.handle(delete);
    }
}
